---
description: "Swift documentation for 'CustomDebugStringConvertible'"
layout: "default"
title: "CustomDebugStringConvertible"
root: "/v5.1"
keywords: ""
---

<div class="intro-declaration"><code class="language-swift">protocol CustomDebugStringConvertible</code></div><p>A type with a customized textual representation suitable for debugging
purposes.</p>
<table class="standard"><tr><th id="conforming-types">Conforming Types</th><td><code class="inherits">AnyHashable, Array, ArraySlice, CVaListPointer, Character, ClosedRange, CodingKey, CollectionOfOne, ContiguousArray, Dictionary, Double, Float, Float80, KeyValuePairs, ObjectIdentifier, OpaquePointer, Optional, Range, SIMD16, SIMD2, SIMD3, SIMD32, SIMD4, SIMD64, SIMD8, Set, StaticString, String, String.UTF16View, String.UTF8View, String.UnicodeScalarView, Substring, Unicode.Scalar, UnsafeBufferPointer, UnsafeMutableBufferPointer, UnsafeMutableRawBufferPointer, UnsafeRawBufferPointer</code></td></tr></table><div class="discussion comment"><p>Swift provides a default debugging textual representation for any type.
That default representation is used by the <code>String(reflecting:)</code>
initializer and the <code>debugPrint(_:)</code> function for types that don't provide
their own. To customize that representation, make your type conform to the
<code>CustomDebugStringConvertible</code> protocol.</p>
<p>Because the <code>String(reflecting:)</code> initializer works for instances of <em>any</em>
type, returning an instance's <code>debugDescription</code> if the value passed
conforms to <code>CustomDebugStringConvertible</code>, accessing a type's
<code>debugDescription</code> property directly or using
<code>CustomDebugStringConvertible</code> as a generic constraint is discouraged.</p>
<blockquote>
<p>Note:
Calling the <code>dump(_:_:_:_:)</code> function and printing in the debugger uses both <code>String(reflecting:)</code> and <code>Mirror(reflecting:)</code> to collect information about an instance. If you implement <code>CustomDebugStringConvertible</code> conformance for your custom type, you may want to consider providing a custom mirror by implementing <code>CustomReflectable</code> conformance, as well.</p>
</blockquote>
<h3>Conforming to the CustomDebugStringConvertible Protocol</h3>
<p>Add <code>CustomDebugStringConvertible</code> conformance to your custom types by
defining a <code>debugDescription</code> property.</p>
<p>For example, this custom <code>Point</code> struct uses the default representation
supplied by the standard library:</p>
<pre><code class="language-swift">struct Point {
    let x: Int, y: Int
}

let p = Point(x: 21, y: 30)
print(String(reflecting: p))
// Prints &quot;p: Point = {
//           x = 21
//           y = 30
//         }&quot;
</code></pre>
<p>After adding <code>CustomDebugStringConvertible</code> conformance by implementing the
<code>debugDescription</code> property, <code>Point</code> provides its own custom debugging
representation.</p>
<pre><code class="language-swift">extension Point: CustomDebugStringConvertible {
    var debugDescription: String {
        return &quot;Point(x: \(x), y: \(y))&quot;
    }
}

print(String(reflecting: p))
// Prints &quot;Point(x: 21, y: 30)&quot;
</code></pre>
</div><h3>Instance Variables</h3><div id="debugDescription-15dc1a07010ea28e38b1f4d74663fe11" class="declaration"><a class="toggle-link" href="#comment-debugDescription-15dc1a07010ea28e38b1f4d74663fe11">var debugDescription</a> <span class="required">Required</span><div class="comment collapse in" id="comment-debugDescription-15dc1a07010ea28e38b1f4d74663fe11"><p>A textual representation of this instance, suitable for debugging.</p>
<p>Calling this property directly is discouraged. Instead, convert an
instance of any type to a string by using the <code>String(reflecting:)</code>
initializer. This initializer works with any type, and uses the custom
<code>debugDescription</code> property for types that conform to
<code>CustomDebugStringConvertible</code>:</p>
<pre><code class="language-swift">struct Point: CustomDebugStringConvertible {
    let x: Int, y: Int

    var debugDescription: String {
        return &quot;(\(x), \(y))&quot;
    }
}

let p = Point(x: 21, y: 30)
let s = String(reflecting: p)
print(s)
// Prints &quot;(21, 30)&quot;
</code></pre>
<p>The conversion of <code>p</code> to a string in the assignment to <code>s</code> uses the
<code>Point</code> type's <code>debugDescription</code> property.</p>
<h4>Declaration</h4><code class="language-swift">var debugDescription: String</code></div></div>